package dao

import (
	"encoding/json"
	"time"
)

type vm struct {
	VmId int `json:"vm_id" gorm:"column:vm_id"`
	Uid int `json:"uid"`
	SnId int `json:"sn_id" gorm:"column:sn_id"`
	Uuid string `json:"uuid" gorm:"column:uuid"`
	Name string `json:"name" gorm:"column:name"`
	Desc string `json:"desc" gorm:"column:description"`
	ZoneUuid string `json:"zone_uuid" gorm:"column:zoneUuid"`
	ClusterUuid string `json:"cluster_uuid" gorm:"column:clusterUuid"`
	ImageUuid string `json:"image_uuid" gorm:"column:imageUuid"`
	HostUuid string `json:"host_uuid" gorm:"column:hostUuid"`
	LastHostUuid string `json:"last_host_uuid" gorm:"column:lastHostUuid"`
	InstanceOfferingUuid string `json:"instance_offering_uuid" gorm:"column:instanceOfferingUuid"`
	RootVolumeUuid string `json:"root_vol_uuid" gorm:"column:rootVolumeUuid"`
	Platform string `json:"platform" gorm:"column:platform"`
	DefaultL3NetworkUuid string `json:"default_l3_uuid" gorm:"column:defaultL3NetworkUuid"`
	Type string `json:"type" gorm:"column:type"`
	HypervisorType string `json:"hypervisor_type" gorm:"column:hypervisorType"`
	MemorySize float64 `json:"memory_size" gorm:"column:memorySize"`
	CpuNum float64 `json:"cpu_num" gorm:"column:cpuNum"`
	AllocatorStrategy string `json:"allocator_strategy" gorm:"column:allocatorStrategy"`
	State string `json:"state" gorm:"column:state"`
	VmNics string `json:"vm_nics" gorm:"column:vmNics"`
	AllVolumes string `json:"all_volumes" gorm:"column:allVolumes"`
	Ctime int64 `json:"c_time" gorm:"column:c_time"`
	Dtime int64 `json:"d_time" gorm:"column:d_time"`
}
func (t *vm)Create(d map[string]interface{}) bool {

	var vma vm
	if v,ok := d["uuid"];ok{
		vma.Uuid = v.(string)
	}
	if v,ok := d["name"];ok{
		vma.Name = v.(string)
	}
	if v,ok := d["desc"];ok{
		vma.Desc = v.(string)
	}
	if v,ok := d["zoneUuid"];ok{
		vma.ZoneUuid = v.(string)
	}
	if v,ok := d["clusterUuid"];ok{
		vma.ClusterUuid = v.(string)
	}
	if v,ok := d["imageUuid"];ok{
		vma.ImageUuid = v.(string)
	}
	if v,ok := d["hostUuid"];ok{
		vma.HostUuid = v.(string)
	}
	if v,ok := d["lastHostUuid"];ok{
		vma.LastHostUuid = v.(string)
	}
	if v,ok := d["instanceOfferingUuid"];ok{
		vma.InstanceOfferingUuid = v.(string)
	}
	if v,ok := d["rootVolumeUuid"];ok{
		vma.RootVolumeUuid = v.(string)
	}
	if v,ok := d["platform"];ok{
		vma.Platform = v.(string)
	}
	if v,ok := d["defaultL3NetworkUuid"];ok{
		vma.DefaultL3NetworkUuid = v.(string)
	}
	if v,ok := d["type"];ok{
		vma.Type = v.(string)
	}
	if v,ok := d["hypervisorType"];ok{
		vma.HypervisorType = v.(string)
	}
	if v,ok := d["memorySize"];ok{
		vma.MemorySize = v.(float64)
	}
	if v,ok := d["cpuNum"];ok{
		vma.CpuNum = v.(float64)
	}
	if v,ok := d["allocatorStrategy"];ok{
		vma.AllocatorStrategy = v.(string)
	}
	if v,ok := d["state"];ok{
		vma.State = v.(string)
	}
	if v,ok := d["vmNics"];ok{
		byte1,_ := json.Marshal(v)
		vma.VmNics = string(byte1)
	}
	if v,ok := d["allVolumes"];ok{
		byte2,_ := json.Marshal(v)
		vma.AllVolumes = string(byte2)
	}
	if v,ok := d["sn_id"];ok{
		vma.SnId = v.(int)
	}
	vma.Uid = d["uid"].(int)

	vma.Ctime = time.Now().Unix()

	err := db.Create(&vma).Error
	b := true
	if err != nil{
		b = false
	}
	return b
}

func (t *vm) FindUuid() (*vm, error){
	table := db.Select("uuid")
	if t.SnId != 0 {
		table = table.Where("sn_id = ?",t.SnId)
	}
	err := table.First(&t).Error
	return t, err
}

var Vm = &vm{}
